<?php
/**
 * aes加解密
 * @author zlj <389843954@qq.com>
 */
namespace app\common\model;
class Aes{
	/**
	 * 秘钥
	 * @param  [string] $id [秘钥下标]
	 */
	public function createKey($id){
		$key = array(
			"0"=>"ehy6m8zyhhsa",
			"1"=>"o3rj96prshzd",
			"2"=>"e9gdo7f7pist",
			"3"=>"3yurwqgwrjxs",
			"4"=>"qm7dgoi4zxsx",
			"5"=>"x5beefxexm3f",
			"6"=>"ougf8wixmy3u",
			"7"=>"hrfpxhhekwkg",
			"8"=>"f8it65oxtuuu",
			"9"=>"25gxu7b9wpq7",
			"10"=>"bxkqkk7evuf5",
			"11"=>"rinddqn589rt",
			"12"=>"hawhpidiz92x",
			"13"=>"y578oeqbrryn",
			"14"=>"9hr9s8gkh9vk",
			"15"=>"v9fr4fu2qjkw",
			"16"=>"usec4wpti4aa",
			"17"=>"r4i8u49kfttu",
			"18"=>"jh3zuuchf4ga",
			"19"=>"txsu7m773tyq",
			"20"=>"kszgcamtefnt",
			"21"=>"dzgwd9v4xtar",
			"22"=>"8ygxrfs4pe47",
			"23"=>"nkrom9mj93y9",
			"24"=>"oaorfk5jm84m",
			"25"=>"uwyd8umsbsbe",
			"26"=>"8wx5iytfc7ub",
			"27"=>"a8c8ec55aajh",
			"28"=>"38gbcv8z6hnk",
			"29"=>"6s4iexeqhqxr",
			"30"=>"jcii5hhhcfpo",
			"31"=>"rthhiwqw6gqg",
			"32"=>"tmc245w738as",
			"33"=>"bmsdxibwjn6t",
			"34"=>"8975jzqan5n8",
			"35"=>"4e9gyyfxvmh5",
			"36"=>"xhb2pd73bfz8",
			"37"=>"bf4ytjbafiym",
			"38"=>"9mxytgjrbcoz",
			"39"=>"9xm6rh87gpuj",
			"40"=>"etd86j3ov4tu",
			"41"=>"67za32ev7dd5",
			"42"=>"mfszyvyybysz",
			"43"=>"afjkwg4nusto",
			"44"=>"7nimgpjyt9rq",
			"45"=>"uwhbmzkpr8ed",
			"46"=>"bqqaxazwo4z9",
			"47"=>"5w3pew4b4udh",
			"48"=>"bwr9mum24urs",
			"49"=>"dnk2jguq7yyb",
			"50"=>"dhsrqnmkdpbo",
			"51"=>"td8j9u678vi3",
			"52"=>"so44kxaspwv4",
			"53"=>"rb9isc8yfq7y",
			"54"=>"rpwgeytxerpc",
			"55"=>"drw5f8cb5u9c",
			"56"=>"mvi6fziaog3m",
			"57"=>"yyipeqpgjgyd",
			"58"=>"if6ewam2sa88",
			"59"=>"r69fx4knwtkc",
			"60"=>"rk3f42dgyhdd",
			"61"=>"2kdkck95h6cg",
			"62"=>"mkhj9jztiasb",
			"63"=>"zh6ftfx7jrh2",
			"64"=>"aqoxm3tdnyrb",
			"65"=>"x6t8nvw2btba",
			"66"=>"28wwfszdkrhm",
			"67"=>"4to628eyzn2g",
			"68"=>"5ceoy2uzmw8h",
			"69"=>"pqvjaon7xgvt",
			"70"=>"tcehgzkmzp96",
			"71"=>"z4rmkhxxci5m",
			"72"=>"cf8m7yhdycsv",
			"73"=>"a9im9whif324",
			"74"=>"wwzac7rjr45w",
			"75"=>"rdgsh8k6hcuh",
			"76"=>"uz4r8vpfn8gh",
			"77"=>"qqrekmf4rns3",
			"78"=>"exqb3cj9uy4v",
			"79"=>"ekqoqebkh663",
			"80"=>"r2464ri9ok9b",
			"81"=>"z7mv8tgvw66r",
			"82"=>"7gijmgwnbb63",
			"83"=>"usmab358cw2e",
			"84"=>"yi7qumwq2e4d",
			"85"=>"5m249gkvxo47",
			"86"=>"cgez2iur45bh",
			"87"=>"5x4k9824zoie",
			"88"=>"jhdjffxyqp2n",
			"89"=>"4z22kks4s6mb",
			"90"=>"twdoakem8ej4",
			"91"=>"a36nit36jdm6",
			"92"=>"gxnku8iwsg45",
			"93"=>"wdooa74iikr7",
			"94"=>"otgjdmj6p6vx",
			"95"=>"a7te5w6gw3am",
			"96"=>"qt2u5yqk7p8t",
			"97"=>"kpu2c9h73zgq",
			"98"=>"u8mobmafidcz",
			"99"=>"493v4gdy6pjs",
			"100"=>"b8hb8piyxegr"
		);

		return $key[$id];
	}

    /** 
     * 加密 
     * @param [string] $key [密钥] 
     * @param [string] $word [明文]
     * @param [string] $method [加密方式   AES128/AES256 ]
     * @return [string] $密文 
     */
    public function encrypt($key,$word,$method='AES128'){
    	
     	$iv_size = mcrypt_get_iv_size($this->method($method), MCRYPT_MODE_ECB);
	    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

	    $ciphertext = mcrypt_encrypt($this->method($method), $key,$word, MCRYPT_MODE_ECB, $iv);
	    $ciphertext = $iv.$ciphertext;
	    // 对密文进行 base64 编码
	    $ciphertext_base64 = base64_encode($ciphertext);

        return $ciphertext_base64;
    }

    /**
	 * 解密
	 * @param [string] $key [密钥] 
     * @param [string] $word [密文]
     * @param [string] $method [加密方式   AES128/AES256 ]
     * @return [string] $plaintext_dec [密文]
	 */
    public function decrypt($key,$word,$method='AES128'){
     	$iv_size = mcrypt_get_iv_size($this->method($method), MCRYPT_MODE_ECB);
		//对密文进行 base64 解码
        $ciphertext_dec = base64_decode($word);
    
	    // 初始向量大小，可以通过 mcrypt_get_iv_size() 来获得
	    $iv_dec = substr($ciphertext_dec, 0, $iv_size);
	    
	    //获取除初始向量外的密文
	    $ciphertext_dec = substr($ciphertext_dec, $iv_size);
	

	    //可能需要从明文末尾移除 0
	    $plaintext_dec = mcrypt_decrypt($this->method($method), $key,$ciphertext_dec, MCRYPT_MODE_ECB, $iv_dec);
		
	    $plaintext_dec = rtrim($plaintext_dec);
	    return $plaintext_dec;
    }
	
	/**
	 * 判断加密方法
	 */
	public function method($method){
		switch ($method)
		{
			case 'AES128':
			  $mcrypt = MCRYPT_RIJNDAEL_128;
			  break;  
			case 'AES256':
			  $mcrypt = MCRYPT_RIJNDAEL_256;
			  break;
			default:
			  break;
		}
		return $mcrypt;
    }

    /**
     * 加密Mu
     * @param  [type] $str [description]
     * @param  [type] $key [description]
     * @return [type]      [description]
     */
    public function aesEncode($key,$str){

	    // if (strpos($str, "\\")){
	    //     $str = str_replace("\\", '', $str);
	    // }
	    // echo $str.'</br>';
	    // $str.=String::randString(8,1);
	    
	    $str .= rand(1000000000,9999999999);

	    $CIPHER = MCRYPT_RIJNDAEL_128;
	    $MODE = MCRYPT_MODE_ECB;
	    $iv = mcrypt_create_iv(mcrypt_get_iv_size($CIPHER, $MODE), MCRYPT_RAND);
	    $out_str = mcrypt_encrypt($CIPHER, $key, $str, $MODE, $iv);
	    // 转换成base64
	//     if (strpos($out_str, "\\")){
	//         $out_str = str_replace("\\", '', $out_str);
	//     }
	    $out_str = base64_encode($out_str);
	    return $out_str;
	}

	/**
     * 加密(二维码)
     * @param  [type] $str [description]
     * @param  [type] $key [description]
     * @return [type]      [description]
     */
    public function aesTwoCode($key,$str){

	    $CIPHER = MCRYPT_RIJNDAEL_128;
	    $MODE = MCRYPT_MODE_ECB;
	    $iv = mcrypt_create_iv(mcrypt_get_iv_size($CIPHER, $MODE), MCRYPT_RAND);
	    $out_str = mcrypt_encrypt($CIPHER, $key, $str, $MODE, $iv);
	    // 转换成base64
	//     if (strpos($out_str, "\\")){
	//         $out_str = str_replace("\\", '', $out_str);
	//     }
	 	
	    $out_str = base64_encode($out_str);
	    return $out_str;
	}

	/**
	 * 解密
	 * @param  [type] $str [description]
	 * @param  [type] $key [description]
	 * @return [type]      [description]
	 */
	public function aesDecode($key,$str){
	    //去掉8个字节的随机数
	    $CIPHER = MCRYPT_RIJNDAEL_128; 
	    $MODE = MCRYPT_MODE_ECB;
	    $string = '';
	    //base64还原
	    $string = base64_decode($str);
	    
	    $iv = mcrypt_create_iv(mcrypt_get_iv_size($CIPHER, $MODE), MCRYPT_RAND);
	    $dec_str = mcrypt_decrypt($CIPHER, $key, $string, $MODE, $iv);
	    //去掉乱码
	    $other_str = substr($dec_str, strlen($dec_str)-1,strlen($dec_str));
	    if (!is_numeric($other_str)){
	        $sub_str = str_replace($other_str, "", $dec_str);
	    }else {
	        $sub_str = $dec_str;
	    }
	    
	    //调试时候注意，这里有问题
	    $other_str = substr($sub_str, strlen($sub_str)-1,strlen($sub_str));
	    if (!is_numeric($other_str)){
	        $sub_str = str_replace($other_str, "", $sub_str);
	    }
	    
	    //去除随机数
	    //$sub_str = substr($sub_str, 0,strlen($sub_str)-8);
	    // if (strpos($sub_str, "\\")){
	    //     $sub_str = str_replace("\\", '', $sub_str);
	    // }
	    
	    
	    $out_str = $sub_str;
	    return $out_str;
	}


    /**
     * APP二维码解密
     * @return [type] [description]
     */
    public function appDec($load,$keyid){
    	//获取秘钥
    	$key = $this->createKey($keyid);

    	$devid = $this->aesDecode($key,$load);

    	$devid = $devid?$devid:false;

    	return $devid;
    }
}